﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using Castle.ActiveRecord;
using System.Web.UI;
using System.IO;
using SimplePMT.Data;
using NHibernate.Expression;

namespace SimplePMT.Control
{
    public partial class TaskContainerControl : System.Web.UI.UserControl
    {
        public string taskContainerLabel = string.Empty;
        public bool moveBuffer = true;
        public List<Task> tasks = new List<Task>();
        public List<Task> moveTasks = new List<Task>();
        public bool edit = false;
        public int taskContainerID = 0;
        public TaskContainer taskContainer;
        public int parentTaskContainerID = 0;
        public int memberID = 0;
        public string title = string.Empty;
        public string jsArray = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        public override string ToString()
        {
            using (new SessionScope())
            {
                
            
                memberID = Convert.ToInt32(Session["MemberID"]);
                taskContainer = TaskContainer.Find(taskContainerID) ?? new TaskContainer();
                if (taskContainerID == 0)
                {
                    edit = true;
                    title = "Add New Task Container";
                } else {
                    title = taskContainer.Label;
                    if (taskContainer.CreatingMember.ID == memberID)
                    {
                        edit = true;
                    }
                }
                if (taskContainer.ID > 0)
                {
                    taskContainerLabel = taskContainer.Label.Replace("\"", "&quot;");
                    int c = 0;

                    jsArray = "var taskArray = new Array();\r\n";
                    foreach (TaskContainerItem item in TaskContainerItem.FindAllByProperty("TaskContainer", taskContainer))
                    {
                        jsArray += "taskArray[" + c + "] = document.containerForm.task" + item.TaskID + ";\r\n";
                        tasks.Add(Task.Find(item.TaskID));
                        c++;
                    }
                    if (moveTasks.Count.Equals(0))
                    {
                        moveBuffer = false;
                    }
                    else
                    {
                        moveBuffer = true;
                        tasks = moveTasks;
                        jsArray = "var taskIdArray = new Array();\r\n";
                        c = 0;
                        foreach (Task task in tasks)
                        {
                            jsArray += "taskIdArray[" + c + "] = " + task.ID + ";\r\n";
                            c++;
                        }
                    }
                }
            }

            string result = "";
            StringWriter writer = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
            this.Render(htmlWriter);
            result = writer.ToString();
            return result;
        }
        
    }
}